import mysql.connector


def execute_query(query_string):
    # 设置数据库连接参数
    conn_params = {
        'host': '127.0.0.1',
        'user': 'root',
        'password': '123456',
        'database': 'world'
    }

    # 创建连接对象
    conn = mysql.connector.connect(**conn_params)

    # 创建游标对象
    cursor = conn.cursor()

    # 执行查询
    cursor.execute(query_string)

    # 获取查询结果的列名
    columns = [column[0] for column in cursor.description]

    # 获取查询结果的所有行数据
    rows = cursor.fetchall()

    # 将查询结果转换成列表，每行数据作为一个字典
    result_set = [dict(zip(columns, row)) for row in rows]

    # 关闭游标和连接
    cursor.close()
    conn.close()

    # 返回查询结果
    return result_set


stmt = """
select 
    a.district_id,
    b.district_name,
    b.district_letter,
	a.id,
    a.city_name,
    a.city_letter
from 
	cn_city as a
    left outer join cn_district as b on a.district_id = b.ID;
"""

result_set = execute_query(stmt)

for r in result_set:
    print(r)
